package bb.lanxing.devicelist.adapter

import android.app.Activity
import android.view.View
import bb.lanxing.R
import bb.lanxing.databinding.DataBindingAdapters
import bb.lanxing.databinding.ItemBikeComputerDeviceListBinding
import bb.lanxing.lib.devices.api.SmartDevice
import bb.lanxing.lib.devices.core.utils.DeviceHelper
import bb.lanxing.util.ui.TypeExtensionsKt
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import kotlin.jvm.internal.Intrinsics

class DevicesBikeComputerAdapter :
  BaseQuickAdapter<SmartDevice, BaseDataBindingHolder<ItemBikeComputerDeviceListBinding>>(
    R.layout.item_bike_computer_device_list
  ), LoadMoreModule {
  override fun convert(
    holder: BaseDataBindingHolder<ItemBikeComputerDeviceListBinding>,
    item: SmartDevice
  ) {
    val dataBinding = holder.dataBinding
    if (dataBinding != null) {
      val imageView = dataBinding.imgDeviceIcon
      val type = item.type
//            val deviceInfoToAddress = DeviceInfo.getDeviceInfoToAddress(item.address)
      DataBindingAdapters.setDeviceIcon(imageView, type, null)
      dataBinding.txtDeviceName.text = item.name
      if (DeviceHelper.isConnected(item.address)) {
        dataBinding.txtDeviceState.setText(R.string.device_detail_state_connected)
        dataBinding.btnUnbind.visibility = View.VISIBLE
        val txtDeviceState = dataBinding.txtDeviceState
        val context = dataBinding.txtDeviceState.context
        TypeExtensionsKt.setDrawableLeft(
          txtDeviceState,
          R.drawable.shape_green_8dp_dot,
          context as Activity
        )
      } else {
        dataBinding.txtDeviceState.setText(R.string.st_offline)
        dataBinding.btnUnbind.visibility = View.GONE
        dataBinding.txtDeviceState.setCompoundDrawablesWithIntrinsicBounds(
          null,
          null,
          null,
          null
        )
      }
    }
  }

  fun getItemPosition(address: String?): Int {
    for (smartDevice in data) {
      if (Intrinsics.areEqual(smartDevice.address, address)) {
        return getItemPosition(smartDevice)
      }
    }
    return -1
  }

  fun removeDevice(address: String?) {
    removeAt(getItemPosition(address))
  }
}